﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class frmThongTinDonHang : Form
    {
        public frmThongTinDonHang()
        {
            InitializeComponent();
        }
        private int _maHD;

        public int MaHD
        {
            set { _maHD = value; }
        }

        private void frmThongTinDonHang_Load(object sender, EventArgs e)
        {
            LayThongTinHoaDon();
        }
        private void LayThongTinHoaDon()
        {
            HoaDonBUS hdBUS = new HoaDonBUS();
            HoaDonDTO hdDTO = new HoaDonDTO();
            ChiTietHoaDonBUS ctBUS = new ChiTietHoaDonBUS();
            hdDTO = hdBUS.ThongTinHoaDonTheoMaL(_maHD);
            List<MatHangDTO> listMH = ctBUS.DanhSachMatHangTheoMaHoaDonL(_maHD);
            TheHienThongTinHoaDon(hdDTO);
            TheHienDanhSachMatHang(hdDTO);
            TheHienThongTinKhachHang(hdDTO);
        }
        private string LoaiHoaDon(int maLoai)
        {
            string loaiHD = "";
            if (maLoai == 0)
            {
                loaiHD = " Đơn đặt hàng ";
            }
            else
            {
                loaiHD = " Đơn bán hàng ";
            }
            return loaiHD;
        }
        private string TinhNgayGiao(HoaDonDTO hdDTO)
        {
            string ng="";
            HoaDonBUS hdBUS = new HoaDonBUS();
            long sophut = hdBUS.LayThoiGianGiaoL(hdDTO.MaHoaDon);
            if (sophut <= 0)
            {
                ng = TinhTrang(hdDTO.TinhTrang);
            }
            else
            {
                long ngay = sophut / 1440;
                sophut = sophut % 1440;
                long gio = sophut / 60;
                sophut = sophut % 60;
                ng = ngay + " Ngày " + gio + " Giờ " + sophut + " Phút ";
            }
            return ng;
        }
        private string TinhTrang(int tt)
        {
            string tinhtrang = "";
            switch (tt)
            {
                case 1: tinhtrang = "Đã giao";
                    break;
                case -1: tinhtrang = "Đã hủy";
                    break;
            }
            return tinhtrang;
        }
        private void TheHienThongTinHoaDon(HoaDonDTO hdDTO)
        {
            txtMaHoaDon.Text = hdDTO.MaHoaDon.ToString();
            txtLoaiHoaDon.Text = LoaiHoaDon(hdDTO.LoaiHoaDon);
            dtpGioLap.Value = hdDTO.NgayLap;
            dtpNgayLap.Value = hdDTO.NgayLap.Date;
            txtMaSoThue.Text = hdDTO.MaSoThue.Trim(); 
            txtVat.Text = hdDTO.Vat.Trim();
            dtpGioGiao.Value = hdDTO.NgayGiao;
            dtpNgayGiao.Value = hdDTO.NgayGiao.Date;
            txtDiaChiGiao.Text = hdDTO.GiaoDen;
            txtPhiGiaoHang.Text = hdDTO.PhiGiaoHang.ToString();
            txtTraTruoc.Text = hdDTO.TraTruoc.ToString();
            txtTongTien.Text = hdDTO.ThanhTien.ToString();
            txtConNo.Text = hdDTO.ConNo.ToString();
            txtThoiGianGiao.Text = TinhNgayGiao(hdDTO);
        }
        private void TheHienDanhSachMatHang(HoaDonDTO hdDTO)
        {
            int stt = dgvDanhSachMatHang.Rows.Count;
            ChiTietHoaDonBUS ctBUS = new ChiTietHoaDonBUS();
            List<MatHangDTO> listMH = ctBUS.DanhSachMatHangTheoMaHoaDonL(hdDTO.MaHoaDon);
            MatHangBUS mhBUS = new MatHangBUS();
            XuatXuBUS xxBUS = new XuatXuBUS();
            DonViTinhBUS dvBUS = new DonViTinhBUS();
            for (int i = 0; i < listMH.Count; i++)
            {
                stt++;
                string tenMH = mhBUS.LayTenMatHangTheoMaL(listMH[i].MaMatHang);
                string tenXX = xxBUS.LayTenXuatXuTheoMaL(listMH[i].MaXuatXu);
                string tenDV = dvBUS.LayTenDonViTinhL(listMH[i].MaDonViTinh);
                int soluong = listMH[i].SoLuong;
                double dongia = listMH[i].GiaNhap;
                double thanhtien = soluong * dongia;
                dgvDanhSachMatHang.Rows.Add(stt, tenMH, tenXX, tenDV, soluong, dongia, thanhtien);
            }
        }
        private void TheHienThongTinKhachHang(HoaDonDTO hdDTO)
        {
            KhachHangBUS khBUS = new KhachHangBUS();
            KhachHangDTO khDTO = new KhachHangDTO();
            khDTO = khBUS.LayTenThongTinKhachHangTheoMaL(hdDTO.MaKhachHang);
            string tenKH = khDTO.TenKhachHang;
            string sodienthoai = khDTO.SoDienThoai;
            txtTenKhachHang.Text = tenKH;
            txtSoDienThoai.Text = sodienthoai;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

     
    }
}
